//
//  GeometryReaderView.swift
//  SwiftUI-CTao
//
//  Created by 涛 on 2019/9/17.
//  Copyright © 2019 涛. All rights reserved.
//

import SwiftUI

struct GeometryReaderView: View {
    
    static let symbolColor = Color(red: 79.0 / 255, green: 79.0 / 255, blue: 191.0 / 255)

    let tmpData: ClosedRange = 0...100
    
    var body: some View {
        
            /// GeometryReader 用法:
            
        VStack {
            /// 1、获取几何布局内的宽，高，相对位置x、y
//            GeometryReader { geometry in
//
//                VStack {
//
//                    ZStack {
//                        Text("第1部分").foregroundColor(.white)
//                        Image("3")
//                            .frame(width: geometry.size.width, height: geometry.size.height/2)
//                    }
//                }
//            }
            
            /// 2、可以return一些View，如做上下拉刷新时候的处理
            List {
                GeometryReader { geometry -> Text in
                    let frame = geometry.frame(in: .global)
                    if frame.origin.y > 200 {
                        return Text("loading")
                    } else {
                        return Text("往下拉。。。")
                    }
                }

                ForEach(tmpData, id: \.self) {data in
                    Text(String(data))
                }
            }

        }

//        GeometryReader { geometry in
//            Path { path in
//                let width = min(geometry.size.width, geometry.size.height)
//                let height = width * 0.75
//                let spacing = width * 0.030
//                let middle = width / 2
//                let topWidth = 0.226 * width
//                let topHeight = 0.488 * height
//
//                path.addLines([
//                    CGPoint(x: middle, y: spacing),
//                    CGPoint(x: middle - topWidth, y: topHeight - spacing),
//                    CGPoint(x: middle, y: topHeight / 2 + spacing),
//                    CGPoint(x: middle + topWidth, y: topHeight - spacing),
//                    CGPoint(x: middle, y: spacing)
//                ])
//
//                path.move(to: CGPoint(x: middle, y: topHeight / 2 + spacing * 3))
//                path.addLines([
//                    CGPoint(x: middle - topWidth, y: topHeight + spacing),
//                    CGPoint(x: spacing, y: height - spacing),
//                    CGPoint(x: width - spacing, y: height - spacing),
//                    CGPoint(x: middle + topWidth, y: topHeight + spacing),
//                    CGPoint(x: middle, y: topHeight / 2 + spacing * 3)
//                ])
//            }
//            .fill(Self.symbolColor)
//        }
    }
}

struct GeometryReaderView_Previews: PreviewProvider {
    static var previews: some View {
        GeometryReaderView()
    }
}
